//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.list;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import org.jpgolay.gui.fixed.SearchBox;
import org.jpgolay.gui.fixed.TableViewer;

public class ListPanel extends JPanel {

    public ListPanel() {
        jContentPane = null;
        searchBox = null;
        contentTableViewer = null;
        infosList = null;
        infoListRight = null;
        infoListCenter = null;
        infoListLeft = null;
        jSplitPane = null;
        columnPanel = null;
        columnTableViewer = null;
        initialize();
    }

    private void initialize() {
        setSize(1000, 400);
        setLayout(new BorderLayout());
        add(getJSplitPane(), "Center");
    }

    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(new BorderLayout());
            jContentPane.add(getSearchBox(), "North");
            jContentPane.add(getContentTableViewer(), "Center");
            jContentPane.add(getInfosList(), "South");
        }
        return jContentPane;
    }

    private SearchBox getSearchBox() {
        if (searchBox == null) {
            searchBox = new SearchBox();
            searchBox.setPreferredSize(searchBox.getSize());
            searchBox.addPropertyChangeListener("icon", new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent e) {
                    searchBox.setPreferredSize(searchBox.getSize());
                    getJContentPane().revalidate();
                }
            });
        }
        return searchBox;
    }

    private TableViewer getContentTableViewer() {
        if (contentTableViewer == null) {
            contentTableViewer = new TableViewer();
        }
        return contentTableViewer;
    }

    private JPanel getInfosList() {
        if (infosList == null) {
            GridLayout gridLayout1 = new GridLayout();
            infosList = new JPanel();
            infosList.setLayout(gridLayout1);
            gridLayout1.setRows(1);
            gridLayout1.setColumns(3);
            gridLayout1.setHgap(2);
            infosList.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
            infosList.add(getInfoListLeft(), null);
            infosList.add(getInfoListCenter(), null);
            infosList.add(getInfoListRight(), null);
        }
        return infosList;
    }

    private JLabel getInfoListRight() {
        if (infoListRight == null) {
            infoListRight = new JLabel();
            infoListRight.setText("info right");
            infoListRight.setHorizontalAlignment(4);
        }
        return infoListRight;
    }

    private JLabel getInfoListCenter() {
        if (infoListCenter == null) {
            infoListCenter = new JLabel();
            infoListCenter.setText("info center");
            infoListCenter.setHorizontalAlignment(0);
        }
        return infoListCenter;
    }

    private JLabel getInfoListLeft() {
        if (infoListLeft == null) {
            infoListLeft = new JLabel();
            infoListLeft.setText("info left");
            infoListLeft.setHorizontalAlignment(2);
        }
        return infoListLeft;
    }

    private JSplitPane getJSplitPane() {
        if (jSplitPane == null) {
            jSplitPane = new JSplitPane();
            jSplitPane.setLeftComponent(getJContentPane());
            jSplitPane.setDividerSize(8);
            jSplitPane.setOneTouchExpandable(true);
            jSplitPane.setRightComponent(getColumnPanel());
            jSplitPane.setDividerLocation(800);
        }
        return jSplitPane;
    }

    private JPanel getColumnPanel() {
        if (columnPanel == null) {
            columnPanel = new JPanel();
            columnPanel.setLayout(new BorderLayout());
            columnPanel.add(getColumnTableViewer(), "Center");
        }
        return columnPanel;
    }

    private TableViewer getColumnTableViewer() {
        if (columnTableViewer == null) {
            columnTableViewer = new TableViewer();
        }
        return columnTableViewer;
    }

    public SearchBox getSearchBoxPanel() {
        return getSearchBox();
    }

    public TableViewer getContentTable() {
        return getContentTableViewer();
    }

    private TableViewer getColumnTable() {
        return getColumnTableViewer();
    }

    public void setInfoRight(String txt) {
        getInfoListRight().setText(txt);
    }

    public String getInfoRight() {
        return getInfoListRight().getText();
    }

    public void setInfoCenter(String txt) {
        getInfoListCenter().setText(txt);
    }

    public String getInfoCenter() {
        return getInfoListCenter().getText();
    }

    public void setInfoLeft(String txt) {
        getInfoListLeft().setText(txt);
    }

    public String getInfoLeft() {
        return getInfoListLeft().getText();
    }

    public void cleanInfoField() {
        setInfoRight("");
        setInfoCenter("");
        setInfoLeft("");
    }
    private JPanel jContentPane;
    private SearchBox searchBox;
    private TableViewer contentTableViewer;
    private JPanel infosList;
    private JLabel infoListRight;
    private JLabel infoListCenter;
    private JLabel infoListLeft;
    private JSplitPane jSplitPane;
    private JPanel columnPanel;
    private TableViewer columnTableViewer;
}
